package com.game.league.task.detail.panel;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.game.base.app.base.BasePanel;
import com.game.base.app.callback.ItemOnClick;
import com.game.base.utils.toast.ToastUtil;
import com.game.base.view.dialog.DialogUtils;
import com.game.league.R;
import com.game.league.databinding.PanelTaskExampleBinding;
import com.game.league.service.InterfaceMgr;
import com.game.league.task.detail.TaskDetailActivity;
import com.game.league.task.detail.adapter.ExampleImgAdapter;
import com.game.league.task.table.Task;
import com.game.league.util.Constants;
import com.game.league.util.SkipUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 示例图片
 * Created by Su on 2019/6/6.
 */
public class ExamplePanel extends BasePanel<PanelTaskExampleBinding> implements ItemOnClick, View.OnClickListener {
    private ExampleImgAdapter imgAdapter;
    private List<String> imgList;  //图片数据
    private Task task = SkipUtil.currentTask;

    @Override
    protected int getLayoutId() {
        return R.layout.panel_task_example;
    }

    public ExamplePanel(Activity activity) {
        super(activity);
        initView();
    }

    private void initView() {
        binding.setClick(this);
        LinearLayoutManager exampleManager = new LinearLayoutManager(activity);
        exampleManager.setOrientation(RecyclerView.HORIZONTAL);
        binding.recyclerExample.setLayoutManager(exampleManager);
        imgAdapter = new ExampleImgAdapter(activity, 1, this);
        binding.recyclerExample.setAdapter(imgAdapter);
    }

    public void refreshData() {
        if (task.getStepImg() == null || task.getStepImg().isEmpty()) {
            binding.llExampleImg.setVisibility(View.GONE);
        } else {
            imgList = task.getStepImg();
            imgAdapter.setItems((ArrayList<String>) imgList);
        }
    }

    @Override
    public void onItemClick(int viewId, int position, Object o) {
        SkipUtil.showBigImgPreview(activity, position, imgList);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                handleStart();
                break;
        }
    }

    private void handleStart() {
        if (task.getRemainNum() == Constants.TASK_MAX_NUM) {
            skipTask(false);
            return;
        }
        DialogUtils.showLoadingDialog(activity, "申请中...");
        InterfaceMgr.getInstance().applyTask(task.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                DialogUtils.closeTipDialog();
                if (e == null) {
                    skipTask(true);
                } else {
                    ToastUtil.showShort("没有抢到，稍后重试");
                }
            }
        });
    }

    /**
     * @param rob 是否是有限份数的任务
     */
    private void skipTask(boolean rob) {
        String link = task.getLink();
        if (!TextUtils.isEmpty(link)) {
            if (rob) {
                ((TaskDetailActivity) getActivity()).isStartTask = true;
            }
            SkipUtil.showWebActivity(activity, link, true);
        }
    }
}
